package com.lm.activiti.boot.tools;

import net.coobird.thumbnailator.Thumbnails;

import java.io.*;

/**
 * @author lm_jcf
 * @date 2021/8/16 16:13
 */
public class ImageUti {

    public static void main(String[] args) throws IOException {

        File file = new File("D:\\南宁二中标准数据模型增量\\语文文科\\1-10份[0分-150分原卷_南宁二中20202021学年度下学期高一期考试（文科）(语文)]\\5班\\133_邓瑛琪_17910504(1).jpg");

        //压缩图片到临时文件
        Thumbnails.of(new FileInputStream(file))
                .scale(1)
                .outputQuality(0.5F)
                .toFile("D:\\南宁二中标准数据模型增量\\语文文科\\1-10份[0分-150分原卷_南宁二中20202021学年度下学期高一期考试（文科）(语文)]\\5班\\comp+133_邓瑛琪_17910504(1).jpg");

    }

    private static byte[] readInputStream(InputStream inStream, float scale, float quality) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        File temp_dir = new File("");
        if (!temp_dir.exists()) {
            temp_dir.mkdirs();
        }
        String tempFileName = System.currentTimeMillis() + ".jpg";

        File image_temp = new File(temp_dir, tempFileName);
        if (!image_temp.exists()) {
            image_temp.createNewFile();
        }
        //压缩图片到临时文件
        Thumbnails.of(inStream)
                .scale(scale)
                .outputQuality(quality)
                .toFile(image_temp);

        FileInputStream is = new FileInputStream(image_temp);

        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = is.read(buffer)) != -1) {
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        is.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }
}
